package com.hckjsec.website.controller;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.hckjsec.common.utils.SecurityUtils;
import com.hckjsec.common.utils.StringUtils;
import com.hckjsec.common.utils.sign.Base64Converter;
import com.hckjsec.website.domain.WebSiteMatterTable;
import com.hckjsec.website.domain.view.WebSiteMatterTableView;
import com.hckjsec.website.service.IWebSiteMatterTableService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.website.domain.WebSiteMatter;
import com.hckjsec.website.service.IWebSiteMatterService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 事项材料Controller
 * 
 * @author cyt
 * @date 2021-10-21
 */
@RestController
@RequestMapping("/system/webmatter")
public class WebSiteMatterController extends BaseController
{
    @Autowired
    private IWebSiteMatterService webSiteMatterService;
    @Autowired
    private IWebSiteMatterTableService webSiteMatterTableService;

    /**
     * 查询事项材料列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WebSiteMatter webSiteMatter)
    {
        startPage();
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            webSiteMatter.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebSiteMatter> list = webSiteMatterService.selectWebSiteMatterList(webSiteMatter);
        list.forEach(kv->{
            kv.setContent(null);
        });
        return getDataTable(list);
    }

    /**
     * 导出事项材料列表
     */
    @Log(title = "事项材料", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WebSiteMatter webSiteMatter)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            webSiteMatter.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebSiteMatter> list = webSiteMatterService.selectWebSiteMatterList(webSiteMatter);
        ExcelUtil<WebSiteMatter> util = new ExcelUtil<WebSiteMatter>(WebSiteMatter.class);
        return util.exportExcel(list, "matter");
    }

    /**
     * 获取事项材料详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        WebSiteMatter webSiteMatter = webSiteMatterService.selectWebSiteMatterById(id);
        List<WebSiteMatterTableView> tableList = new ArrayList<>();
        Long mattId = webSiteMatter.getId();
        WebSiteMatterTable webSiteMatterTable = new WebSiteMatterTable(mattId);
        List<WebSiteMatterTable> list = webSiteMatterTableService.selectWebSiteMatterTableList(webSiteMatterTable);
        list.forEach(kv->{
            WebSiteMatterTableView webSiteMatterTableView = new WebSiteMatterTableView();
            webSiteMatterTableView.setName(kv.getTableName());
            webSiteMatterTableView.setUrl(kv.getNamePath());
            tableList.add(webSiteMatterTableView);
        });
        webSiteMatter.setTableList(tableList);
        return AjaxResult.success(webSiteMatter);
    }

    /**
     * 新增事项材料
     */
    @Log(title = "事项材料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WebSiteMatter webSiteMatter)
    {
        if (!"".equals(StringUtils.isHcNull(webSiteMatter.getContent(),""))){//解密
            webSiteMatter.setContent(Base64Converter.decode(webSiteMatter.getContent()));
        }else{
            webSiteMatter.setContent(webSiteMatter.getContent());
        }
        webSiteMatter.setDeptId(SecurityUtils.getDeptId());
        webSiteMatter.setOperator(SecurityUtils.getUserId());
        int rows = webSiteMatterService.insertWebSiteMatter(webSiteMatter);
        Long mattId = webSiteMatter.getId();
        WebSiteMatterTable webSiteMatterTable = new WebSiteMatterTable(mattId);
        List<WebSiteMatterTable> list = webSiteMatterTableService.selectWebSiteMatterTableList(webSiteMatterTable);
        list.forEach(kv->{
            webSiteMatterTableService.deleteWebSiteMatterTableById(kv.getId());
        });
        if (!StringUtils.isEmpty(webSiteMatter.getTableList()) ){
            List<WebSiteMatterTableView> tableList = webSiteMatter.getTableList();
            tableList.forEach(kv->{
                WebSiteMatterTable webSiteMatterTable1 = new WebSiteMatterTable();
                webSiteMatterTable1.setDeptId(SecurityUtils.getDeptId());
                webSiteMatterTable1.setMatterId(mattId);
                webSiteMatterTable1.setTableName(kv.getName());
                webSiteMatterTable1.setNamePath(kv.getUrl());
                webSiteMatterTable1.setOperator(SecurityUtils.getUserId());
                webSiteMatterTableService.insertWebSiteMatterTable(webSiteMatterTable1);
            });
        }
        return toAjax(rows);
    }

    /**
     * 修改事项材料
     */
    @Log(title = "事项材料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WebSiteMatter webSiteMatter)
    {
        if (!"".equals(StringUtils.isHcNull(webSiteMatter.getContent(),""))){//解密
            webSiteMatter.setContent(Base64Converter.decode(webSiteMatter.getContent()));
        }else{
            webSiteMatter.setContent(webSiteMatter.getContent());
        }
        int rows = webSiteMatterService.updateWebSiteMatter(webSiteMatter);
        Long mattId = webSiteMatter.getId();
        WebSiteMatterTable webSiteMatterTable = new WebSiteMatterTable(mattId);
        List<WebSiteMatterTable> list = webSiteMatterTableService.selectWebSiteMatterTableList(webSiteMatterTable);
        list.forEach(kv->{
            webSiteMatterTableService.deleteWebSiteMatterTableById(kv.getId());
        });
        if (!StringUtils.isEmpty(webSiteMatter.getTableList()) ){
            List<WebSiteMatterTableView> tableList = webSiteMatter.getTableList();
            tableList.forEach(kv->{
                WebSiteMatterTable webSiteMatterTable1 = new WebSiteMatterTable();
                webSiteMatterTable1.setDeptId(SecurityUtils.getDeptId());
                webSiteMatterTable1.setMatterId(mattId);
                webSiteMatterTable1.setTableName(kv.getName());
                webSiteMatterTable1.setNamePath(kv.getUrl());
                webSiteMatterTable1.setOperator(SecurityUtils.getUserId());
                webSiteMatterTableService.insertWebSiteMatterTable(webSiteMatterTable1);
            });
        }
        return toAjax(rows);
    }

    /**
     * 删除事项材料
     */
    @Log(title = "事项材料", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(webSiteMatterService.deleteWebSiteMatterByIds(ids));
    }
}
